package com.lgy.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DownloadUtils {
	private static Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
	/**
	 * 获取远程文件的大小Content-length的长度
	 */
	public static long getRemoteFileSize(String remoteFileUrl) throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost
		HttpGet httpget = new HttpGet(remoteFileUrl);
		try {
			CloseableHttpResponse httpResponse = httpclient.execute(httpget);
			// 获取响应消息实体
			HttpEntity entity = httpResponse.getEntity();
			if(entity != null) {
				long contentLength = entity.getContentLength();
				logger.debug("文件大小ContentLength:" + contentLength);
				return contentLength;
			}
			httpResponse.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}

	/**
	 * 获取远程文件的类型Content-Type
	 * 
	 */
	public static String getRemoteFileType(String remoteFileUrl) throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost
		HttpGet httpget = new HttpGet(remoteFileUrl);
		try {
			CloseableHttpResponse httpResponse = httpclient.execute(httpget);
			// 获取响应消息实体
			HttpEntity entity = httpResponse.getEntity();
			if(entity != null) {
				Header contentType = entity.getContentType();
				logger.debug("文件Content-Type:" + contentType.getValue());
				return contentType.getValue();
			}
			httpResponse.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 
	 * <p>Title: getBytesFromFile</p>
	 * <p>Description: 将文件转化为字节流</p>
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	public  static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        // 获取文件大小
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            // 文件太大，无法读取
        	throw new IOException("File is to large "+file.getName());
        }
        // 创建一个数据来保存文件数据
        byte[] bytes = new byte[(int)length];
        // 读取数据到byte数组中
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        // 确保所有数据均被读取
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
}
